package cn.neusoft.luofa603.yuejian_29.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.List;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.adapter.MasterAdapter;
import cn.neusoft.luofa603.yuejian_29.activity.MasterInfoActivity;
import cn.neusoft.luofa603.yuejian_29.bean.Master;
import cn.neusoft.luofa603.yuejian_29.model.MasterModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MasterFragment extends Fragment {
    private View view;
    public Master master;
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_master, container, false);
        initviews();
        initEvents();
        initData();
        return view;
    }
    public void initviews(){
        recyclerView = (RecyclerView) view.findViewById(R.id.master_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());


    }
    public void initEvents(){
        MasterModel masterModel = new MasterModel();
        Call<List<Master>> call = masterModel.getMaster();
        call.enqueue(new Callback<List<Master>>() {
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response) {
                final List<Master> mDataList = response.body();
                MasterAdapter masterAdapter = new MasterAdapter(getActivity(),mDataList);
                recyclerView.setAdapter(masterAdapter);
                masterAdapter.setmItemClickListener(new MasterAdapter.MyItemClickListener(){

                    @Override
                    public void onItemClick(View view, int position) {

                        Intent intent = new Intent();
                        intent.setClass(getActivity(), MasterInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("Master",mDataList.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);

                    }
                });
            }

            @Override
            public void onFailure(Call<List<Master>> call, Throwable t) {
                Toast.makeText(getActivity(),"与服务器通讯失败！",Toast.LENGTH_SHORT).show();

            }
        });


    }
    public void initData(){

    }

}
